<?php
if( $_GET['msg'] == 'getaptstat' ) {
	//skiljesträng mellan apt-körningar
	$tag = '^\^NEW RUN: \w+\n';

	//läs in fil
	$file = file_get_contents('/var/log/apt');
	//matcha sista körningen
	preg_match('#'.$tag.'([^^]*)\Z#ms', $file, $matches);

	//plocka ut sista raderna
	$lines = explode("\n", $matches[1]);
	$tail = array_slice($lines, -13, 13);
	//dölj lösenord
	$str = preg_replace('#(https://[^:]+:)[^@]+(@www.domain.se)/repository#', '\1[hidden]\2', $tail);
	echo '<i>'.nl2br(htmlentities(implode("\n", $str), ENT_COMPAT, 'UTF-8')).'</i>';

	//kolla om kört klart
	exec('pgrep -f "/apt_install_wwwadmin "', $duh, $runningAptInstall);
	exec('pgrep -f "/apt_upgrade_wwwadmin "', $duh, $runningAptUpgrade);
	$runningAptInstall = !$runningAptInstall;
	$runningAptUpgrade = !$runningAptUpgrade;
	if( !$runningAptInstall && !$runningAptUpgrade)
		echo "\n^Done!";
}
?>
